package com.fyzk.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;
import com.fyzk.activity.lottery.recommend_order.OtherCenterActivity;
import com.fyzk.activity.lottery.recommend_order.RankBean;
import com.fyzk.adapter.RankAdapter;
import com.fyzk.base.BaseFragment;
import com.fyzk.net.Commands;
import com.fyzk.utils.DensityUtils;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.DividerItemDecoration;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by Administrator on 2020/12/10.
 */
@ContentView(R.layout.fragment_rank_list)
public class RankListFragment extends BaseFragment {
    @ViewInject(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @ViewInject(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    private int mType;
    private View mEmptyView;
    private TextView mTvCocumentaryExplain;
    private TextView mTvGoDocumentary;

    private RankAdapter mAdapter;

    public static RankListFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        RankListFragment fragment = new RankListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null != getArguments()) {
            mType = getArguments().getInt("type");
        }
        return x.view().inject(this, inflater, container);//xutil注解;
    }

    private void initViews() {
        mAdapter = new RankAdapter(mType);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(DensityUtils.dip2px(getActivity(),1)));
        mAdapter.bindToRecyclerView(mRecyclerView);
        mEmptyView = LayoutInflater.from(getContext()).inflate(R.layout.item_empty_documentary, mRecyclerView, false);
        mTvCocumentaryExplain = mEmptyView.findViewById(R.id.tv_documentary_explain);
        mTvGoDocumentary = mEmptyView.findViewById(R.id.tv_go_documentary);
        mTvCocumentaryExplain.setText("暂无该榜单数据");
        mTvGoDocumentary.setVisibility(View.GONE);
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView);

        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                findNewRecommendOrders(false);
            }
        });

        mRecyclerView.addOnItemTouchListener(new RVOnItemClickListener());

        refresh();
    }

    private void refresh() {
        mSmartRefreshLayout.autoRefresh();
    }

    private void finishRefresh() {
        mSmartRefreshLayout.finishRefresh();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
    }

    /**
     * 查询晒单大厅 最新推荐
     *
     * @param showProgress
     */
    private void findNewRecommendOrders(boolean showProgress) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("raceType", mType);    // 类型（=1连红榜，=2命中榜，=3盈利榜，=4人气榜）
            httpPostJson(CommandUtil.FINDRECOMMENDPOWERRANKING, Commands.FINDRECOMMENDPOWERRANKING, request_json,
                    showProgress);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        List<RankBean> beans = JSON.parseArray(resultBean.getData(), RankBean.class);
        finishRefresh();
        mAdapter.setNewData(beans);
    }

    @Override
    public void onFailure(int requestCode, String errorCode, String message) {
        finishRefresh();
        mAdapter.setNewData(null);
    }


    @Override
    protected void onFailure(int requestCode, String message) {
        super.onFailure(requestCode, message);
        finishRefresh();
        mAdapter.setNewData(null);
    }

    /**
     * RecyclerView的点击事件
     */
    private class RVOnItemClickListener extends SimpleClickListener {

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            RankBean item = mAdapter.getItem(position);
            Intent intent = new Intent(getContext(), OtherCenterActivity.class);
            if (null != item) {
                intent.putExtra(IntentParamUtils.USERNO, item.getUserno());
                startActivity(intent);
            }
        }

        @Override
        public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {

        }

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        }

        @Override
        public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {

        }
    }
}
